//
//  ConclusionController.swift
//  PoomsaeScringAll
//
//  Created by Илья Проскурнев on 22/02/2019.
//  Copyright © 2019 Илья Проскурнев. All rights reserved.
//

import UIKit

class ConclusionController: UIViewController {
    
    var idvistuplenie: Int!
    var idphumse: String!
    var idhost: String!
    
    var row: Int!
    var success: Int!
    
    var successStr: Int!
    
    var sudNum: Int!
    var kolSud: Int!
    var pokaz: Int!
    var vistocen: Int!
    
    var sud: Int!
    var kolMin = 0
    var kolMax = 0
    
    var freestyleViv: Int!
    
    var sys: Int!
    var postParameters: String!
    
    var summ1 = 0.0
    var summ2 = 0.0
    
    var URL_VIST2: String? = "/php_isap/vistup/vistupsav_ios.php"
    var URL_VIST: String?
    
    var URL_OCENKI2: String? = "/php_isap/ocen/ocenki.php"
    var URL_OCENKI: String?
    
    var URL_VISTUP: String?
    let URL_VISTUPLENIE2 = "/php_isap/vistup/vistuplenie.php"
    
    var idvistup: String!
    var sudNomer: String!
    
    var Acd, minT, maxT, minP, maxP, sum, tot: Double!
    var presentationOc, numPokaz: Int!
    var oct: String!
    var ocp: String!
    var ot: Double!
    var op: Double!
    var zap1 = 0
    var zap2 = 0
    var zap3 = 0
    var zap4 = 0
    var zap5 = 0
    var zap6 = 0
    var zap7 = 0
    
    var attributeString: NSMutableAttributedString?
    
    var ocenki_arr1, ocenki_arr2: NSArray!
    
    var sumT = [Double]()
    var sumP = [Double]()
    
    var ocentp = Double(0)
    
    var kort, kolSudKort: Int!
    var kolSudString: Int!
    
    @IBOutlet weak var t11: UILabel!
    @IBOutlet weak var t12: UILabel!
    @IBOutlet weak var t13: UILabel!
    @IBOutlet weak var t14: UILabel!
    @IBOutlet weak var t15: UILabel!
    @IBOutlet weak var t16: UILabel!
    @IBOutlet weak var t17: UILabel!
    
    @IBOutlet weak var p11: UILabel!
    @IBOutlet weak var p12: UILabel!
    @IBOutlet weak var p13: UILabel!
    @IBOutlet weak var p14: UILabel!
    @IBOutlet weak var p15: UILabel!
    @IBOutlet weak var p16: UILabel!
    @IBOutlet weak var p17: UILabel!
    
    @IBOutlet weak var t21: UILabel!
    @IBOutlet weak var t22: UILabel!
    @IBOutlet weak var t23: UILabel!
    @IBOutlet weak var t24: UILabel!
    @IBOutlet weak var t25: UILabel!
    @IBOutlet weak var t26: UILabel!
    @IBOutlet weak var t27: UILabel!
    
    @IBOutlet weak var p21: UILabel!
    @IBOutlet weak var p22: UILabel!
    @IBOutlet weak var p23: UILabel!
    @IBOutlet weak var p24: UILabel!
    @IBOutlet weak var p25: UILabel!
    @IBOutlet weak var p26: UILabel!
    @IBOutlet weak var p27: UILabel!
    
    @IBOutlet weak var tot1: UILabel!
    @IBOutlet weak var tot2: UILabel!
    @IBOutlet weak var total: UILabel!
    
    var URL_KORT: String?
    let URL_KORT1 = "https://"
    let URL_KORT2 = "/php/kort.php"
    
    var URL_CON: String?
    let URL_CON1 = "https://"
    let URL_CON2 = "/php/while_connect.php"
    
    
    var myJSON1, myJSON2, vistup, vistuplenie, tp: NSDictionary!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.navigationItem.setHidesBackButton(true, animated: true)
//        self.navigationController?.isNavigationBarHidden = true
        
        t21.isHidden = true
        t22.isHidden = true
        t23.isHidden = true
        t24.isHidden = true
        t25.isHidden = true
        t26.isHidden = true
        t27.isHidden = true
        
        p21.isHidden = true
        p22.isHidden = true
        p23.isHidden = true
        p24.isHidden = true
        p25.isHidden = true
        p26.isHidden = true
        p27.isHidden = true
        
        switch kolSud {
        case 3:
            
            t11.isHidden = true
            t12.isHidden = true
            t16.isHidden = true
            t17.isHidden = true
            
            p11.isHidden = true
            p12.isHidden = true
            p16.isHidden = true
            p17.isHidden = true
            
        case 5:
            t11.isHidden = true
            t17.isHidden = true
            
            p11.isHidden = true
            p17.isHidden = true
        default:
            break
        }
        tot2.isHidden = true
        tot1.isHidden = true
        total.isHidden = true
        self.numPokaz = 0
        ocenki1()
        
    }
    
    func ocenki1() {
        self.URL_OCENKI = self.idhost! + self.URL_OCENKI2!
        let requestURL = NSURL(string: self.URL_OCENKI!)
        let request = NSMutableURLRequest(url: requestURL! as URL)
        request.httpMethod = "POST"
        if (self.pokaz == 2) {
            self.postParameters = "vistuplenie_idvistuplenie=" + String (self.idvistuplenie-1)
            DispatchQueue.main.async {
                self.labelOcen()
            }
        } else { self.postParameters = "vistuplenie_idvistuplenie=" + String (self.idvistuplenie) }
        request.httpBody = postParameters.data(using: String.Encoding.utf8)
        self.myJSON1 = NSDictionary()
        let task = URLSession.shared.dataTask(with: request as URLRequest){
            data, response, error in
            do {
                if (error != nil) {
                    DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1), execute: {
                        self.ocenki1()
                    })
                } else {
                    self.myJSON1 =  try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.allowFragments) as? NSDictionary
                    self.success = (self.myJSON1["success"]) as? Int
                    if (self.success > 0) {
                        
                        self.row = (self.myJSON1["row"]) as? Int
                        self.ocenki_arr1 = (self.myJSON1["ocenki"]) as? NSArray
                        DispatchQueue.main.async {
                        switch (self.kolSud) {
                            case 3:
                            for i in self.ocenki_arr1 {
                                self.tp = i as? NSDictionary
                                switch (self.tp["ocenki_nomsud"] as? String) {
                                case "1":
                                    if (self.zap1 == 0) {
                                        self.ocen()
                                        self.t13.text = String (self.ot/Double(10))
                                        self.p13.text = String (self.op/Double(10))
                                        self.zap1 = 1
                                    }
                                    break
                                case "2":
                                    if (self.zap2 == 0) {
                                        self.zap2 = 1
                                        self.ocen()
                                        self.t14.text = String (self.ot/Double(10))
                                        self.p14.text = String (self.op/Double(10))
                                    }
                                    break
                                case "3":
                                    if (self.zap3 == 0) {
                                        self.zap3 = 1
                                        self.ocen()
                                        self.t15.text = String (self.ot/Double(10))
                                        self.p15.text = String (self.op/Double(10))
                                    }
                                    break
                                default:
                                    break
                                }
                            }
                            break

                            case 5:
                            for i in self.ocenki_arr1 {
                                self.tp = i as? NSDictionary
                                switch (self.tp["ocenki_nomsud"] as? String) {
                                case "1":
                                    if (self.zap1 == 0) {
                                        self.zap1 = 1
                                        self.ocen()
                                        self.t12.text = String (self.ot/Double(10))
                                        self.p12.text = String (self.op/Double(10))
                                    }
                                    break
                                case "2":
                                    if (self.zap2 == 0) {
                                        self.zap2 = 1
                                        self.ocen()
                                        self.t13.text = String (self.ot/Double(10))
                                        self.p13.text = String (self.op/Double(10))
                                    }
                                    break
                                case "3":
                                    if (self.zap3 == 0) {
                                        self.zap3 = 1
                                        self.ocen()
                                        self.t14.text = String (self.ot/Double(10))
                                        self.p14.text = String (self.op/Double(10))
                                    }
                                    break
                                case "4":
                                    if (self.zap4 == 0) {
                                        self.zap4 = 1
                                        self.ocen()
                                        self.t15.text = String (self.ot/Double(10))
                                        self.p15.text = String (self.op/Double(10))
                                    }
                                    break
                                case "5":
                                    if (self.zap5 == 0) {
                                        self.zap5 = 1
                                        self.ocen()
                                        self.t16.text = String (self.ot/Double(10))
                                        self.p16.text = String (self.op/Double(10))
                                    }
                                    break
                                default:
                                    break
                                }
                            }

                            case 7:
                                for i in self.ocenki_arr1 {
                                    self.tp = i as? NSDictionary
                                    switch (self.tp["ocenki_nomsud"] as? String) {
                                    case "1":
                                        if (self.zap1 == 0) {
                                            self.zap1 = 1
                                            self.ocen()
                                            self.t11.text = String (self.ot/Double(10))
                                            self.p11.text = String (self.op/Double(10))
                                        }
                                        break
                                    case "2":
                                        if(self.zap2 == 0) {
                                            self.zap2 = 1
                                            self.ocen()
                                            self.t12.text = String (self.ot/Double(10))
                                            self.p12.text = String (self.op/Double(10))
                                        }
                                        break
                                    case "3":
                                        if (self.zap3 == 0) {
                                            self.zap3 = 1
                                            self.ocen()
                                            self.t13.text = String (self.ot/Double(10))
                                            self.p13.text = String (self.op/Double(10))
                                        }
                                        break
                                    case "4":
                                        if (self.zap4 == 0) {
                                            self.zap4 = 1
                                            self.ocen()
                                            self.t14.text = String (self.ot/Double(10))
                                            self.p14.text = String (self.op/Double(10))
                                        }
                                        break
                                    case "5":
                                        if (self.zap5 == 0) {
                                            self.zap5 = 1
                                            self.ocen()
                                            self.t15.text = String (self.ot/Double(10))
                                            self.p15.text = String (self.op/Double(10))
                                        }
                                        break
                                    case "6":
                                        if (self.zap6 == 0) {
                                            self.zap6 = 1
                                            self.ocen()
                                            self.t16.text = String (self.ot/Double(10))
                                            self.p16.text = String (self.op/Double(10))
                                        }
                                        break
                                    case "7":
                                        if (self.zap7 == 0) {
                                            self.zap7 = 1
                                            self.ocen()
                                            self.t17.text = String (self.ot/Double(10))
                                            self.p17.text = String (self.op/Double(10))
                                        }
                                        break
                                    default:
                                        break
                                    }
                                }
                                break
                        default:
                            for i in self.ocenki_arr1 {
                                self.tp = i as? NSDictionary
                                switch (self.tp["ocenki_nomsud"] as? String) {
                                case "1":
                                    self.ocen()
                                    self.t14.text = String (self.ot/Double(10))
                                    self.p14.text = String (self.op/Double(10))
                                    break
                                default:
                                    break
                                }
                            }
                            break
                            }
                        }
                    }
                }
                
            } catch {
                print(error)
            }
            
            if (self.pokaz == 1) {
                if (self.kolSud == self.row) { DispatchQueue.main.async { self.total.isHidden = false; self.numPokaz = 1; self.summa() } } else {
                    DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1), execute: {
                        self.ocenki1()
                    })
                }
            } else { DispatchQueue.main.async {self.numPokaz = 1; self.summa(); self.ocenki2()} }
            
        }
        
        task.resume()
        
    }
    
    func ocen() {
        self.oct = self.tp["ocenki_t"] as? String
        self.ocp = self.tp["ocenki_p"] as? String
        self.ot = Double (self.oct) ?? 0
        self.op = Double (self.ocp) ?? 0
        self.sumT.append(self.ot)
        self.sumP.append(self.op)
    }
    
    func summa() {
        switch pokaz {
        case 1:
            if (kolSud <= 3) {
                let sum = Double (self.sumT.reduce(0, +) + self.sumP.reduce(0, +))/Double(self.kolSud*10)
                self.total.text = String(format:"%.3f", sum)
            }
            if (kolSud > 3) {
                self.minT = self.sumT.min() ?? 0
                self.maxT = self.sumT.max() ?? 0
                self.minP = self.sumP.min() ?? 0
                self.maxP = self.sumP.max() ?? 0
                self.zachtmin()
                self.zachtmax()
                self.zachpmin()
                self.zachpmax()
                let summinmax = self.minT+self.maxT+self.minP+self.maxP
                let kolS = (kolSud - 2)
                sum = self.sumT.reduce(0, +) + self.sumP.reduce(0, +) - summinmax
                let tot = Double(sum)/Double(kolS)
                self.total.text = String(format:"%.3f", tot/10)
            }
            self.vistsav()
            break
        case 2:
            if (kolSud <= 3) {
                if (numPokaz == 1) {
                    summ1 = Double (self.sumT.reduce(0, +) + self.sumP.reduce(0, +))/Double(self.kolSud*10)
                }
                if (numPokaz == 2) {
                    summ2 = Double (self.sumT.reduce(0, +) + self.sumP.reduce(0, +))/Double(self.kolSud*10)
                    let tot = (summ1 + summ2)/Double(2)
                    self.tot1.text = String(format:"%.3f", summ1)
                    self.tot2.text = String(format:"%.3f", summ2)
                    self.total.text = String(format:"%.3f", tot)
                }
            }
            if (kolSud > 3) {
                if (numPokaz == 1) {
                    self.minT = self.sumT.min() ?? 0
                    self.maxT = self.sumT.max() ?? 0
                    self.minP = self.sumP.min() ?? 0
                    self.maxP = self.sumP.max() ?? 0
                    self.zachtmin()
                    self.zachtmax()
                    self.zachpmin()
                    self.zachpmax()
                    let summinmax = self.minT+self.maxT+self.minP+self.maxP
                    let kolS = (kolSud - 2) * 10
                    self.summ1 = self.sumT.reduce(0, +) + self.sumP.reduce(0, +) - summinmax
                    summ1 = Double(summ1)/Double(kolS)
                }
                if (numPokaz == 2) {
                    self.kolMin = 0
                    self.kolMax = 0
                    self.minT = self.sumT.min()
                    self.maxT = self.sumT.max()
                    self.minP = self.sumP.min()
                    self.maxP = self.sumP.max()
                    self.zachtmin()
                    self.zachtmax()
                    self.zachpmin()
                    self.zachpmax()
                    let summinmax = self.minT+self.maxT+self.minP+self.maxP
                    let kolS = (kolSud - 2) * 10
                    summ2 = self.sumT.reduce(0, +) + self.sumP.reduce(0, +) - summinmax
                    summ2 = Double(summ2)/Double(kolS)
                    let tot = (summ1 + summ2)/Double(2)
                    self.tot1.text = String(format:"%.3f", summ1)
                    self.tot2.text = String(format:"%.3f", summ2)
                    self.total.text = String(format:"%.3f", tot)
                }
            }
            self.vistsav()
            break
        default: break
        }
    }
    
    func zach(str: String) {
        attributeString =  NSMutableAttributedString(string: str)
        attributeString!.addAttribute(NSAttributedString.Key.strikethroughStyle, value: NSUnderlineStyle.single.rawValue, range: NSMakeRange(0, attributeString!.length))
    }
    
    func zachtmin() {
        if (numPokaz == 1) {
            if let text = t11.text { ocentp = Double(text)! * Double(10) }
            if (ocentp == self.minT && self.kolMin == 0) {
                kolMin = 1
                zach(str: t11.text!)
                self.t11.attributedText = attributeString
            }
        if let text = t12.text { ocentp = Double(text)! * Double(10) }
            if (ocentp == self.minT && self.kolMin == 0) {
                kolMin = 1
                zach(str: t12.text!)
                self.t12.attributedText = attributeString
            }
            if let text = t13.text { ocentp = Double(text)! * Double(10) }
            if (ocentp == self.minT && self.kolMin == 0) {
                kolMin = 1
                zach(str: t13.text!)
                self.t13.attributedText = attributeString
            }
            if let text = t14.text { ocentp = Double(text)! * Double(10) }
            if (ocentp == self.minT && self.kolMin == 0) {
                kolMin = 1
                zach(str: t14.text!)
                self.t14.attributedText = attributeString
            }
            if let text = t15.text { ocentp = Double(text)! * Double(10) }
            if (ocentp == self.minT && self.kolMin == 0) {
                kolMin = 1
                zach(str: t15.text!)
                self.t15.attributedText = attributeString
            }
            if let text = t16.text { ocentp = Double(text)! * Double(10) }
            if (ocentp == self.minT && self.kolMin == 0) {
                kolMin = 1
                zach(str: t16.text!)
                self.t16.attributedText = attributeString
            }
            if let text = t17.text { ocentp = Double(text)! * Double(10) }
            if (ocentp == self.minT && self.kolMin == 0) {
                kolMin = 1
                zach(str: t17.text!)
                self.t17.attributedText = attributeString
            }
        }
        
        if (numPokaz == 2) {
            if let text = t21.text { ocentp = Double(text)! * Double(10) }
            if (ocentp == self.minT && self.kolMin == 0) {
                kolMin = 1
                zach(str: t21.text!)
                self.t21.attributedText = attributeString
            }
            if let text = t22.text { ocentp = Double(text)! * Double(10) }
            if (ocentp == self.minT && self.kolMin == 0) {
                kolMin = 1
                zach(str: t22.text!)
                self.t22.attributedText = attributeString
            }
            if let text = t23.text { ocentp = Double(text)! * Double(10) }
            if (ocentp == self.minT && self.kolMin == 0) {
                kolMin = 1
                zach(str: t23.text!)
                self.t23.attributedText = attributeString
            }
            if let text = t24.text { ocentp = Double(text)! * Double(10) }
            if (ocentp == self.minT && self.kolMin == 0) {
                kolMin = 1
                zach(str: t24.text!)
                self.t24.attributedText = attributeString
            }
            if let text = t25.text { ocentp = Double(text)! * Double(10) }
            if (ocentp == self.minT && self.kolMin == 0) {
                kolMin = 1
                zach(str: t25.text!)
                self.t25.attributedText = attributeString
            }
            if let text = t26.text { ocentp = Double(text)! * Double(10) }
            if (ocentp == self.minT && self.kolMin == 0) {
                kolMin = 1
                zach(str: t26.text!)
                self.t26.attributedText = attributeString
            }
            if let text = t27.text { ocentp = Double(text)! * Double(10) }
            if (ocentp == self.minT && self.kolMin == 0) {
                kolMin = 1
                zach(str: t27.text!)
                self.t27.attributedText = attributeString
            }
        }
    }
    
    func zachtmax() {
        if (numPokaz == 1) {
            if let text = t11.text { ocentp = Double(text)! * Double(10) }
            if (ocentp == self.maxT && self.kolMax == 0) {
                kolMax = 1
                zach(str: t11.text!)
                self.t11.attributedText = attributeString
            }
            if let text = t12.text { ocentp = Double(text)! * Double(10) }
            if (ocentp == self.maxT && self.kolMax == 0) {
                kolMin = 1
                zach(str: t12.text!)
                self.t12.attributedText = attributeString
            }
            if let text = t13.text { ocentp = Double(text)! * Double(10) }
            if (ocentp == self.maxT && self.kolMax == 0) {
                kolMax = 1
                zach(str: t13.text!)
                self.t13.attributedText = attributeString
            }
            if let text = t14.text { ocentp = Double(text)! * Double(10) }
            if (ocentp == self.maxT && self.kolMax == 0) {
                kolMax = 1
                zach(str: t14.text!)
                self.t14.attributedText = attributeString
            }
            if let text = t15.text { ocentp = Double(text)! * Double(10) }
            if (ocentp == self.maxT && self.kolMax == 0) {
                kolMax = 1
                zach(str: t15.text!)
                self.t15.attributedText = attributeString
            }
            if let text = t16.text { ocentp = Double(text)! * Double(10) }
            if (ocentp == self.maxT && self.kolMax == 0) {
                kolMax = 1
                zach(str: t16.text!)
                self.t16.attributedText = attributeString
            }
            if let text = t17.text { ocentp = Double(text)! * Double(10) }
            if (ocentp == self.maxT && self.kolMax == 0) {
                kolMax = 1
                zach(str: t17.text!)
                self.t17.attributedText = attributeString
            }
        }
        if (numPokaz == 2) {
            if let text = t21.text { ocentp = Double(text)! * Double(10) } else { ocentp = 0 }
            if (ocentp == self.maxT && self.kolMax == 0) {
                kolMax = 1
                zach(str: t21.text!)
                self.t21.attributedText = attributeString
            }
            if let text = t22.text { ocentp = Double(text)! * Double(10) } else { ocentp = 0 }
            if (ocentp == self.maxT && self.kolMax == 0) {
                kolMin = 1
                zach(str: t22.text!)
                self.t22.attributedText = attributeString
            }
            if let text = t23.text { ocentp = Double(text)! * Double(10) } else { ocentp = 0 }
            if (ocentp == self.maxT && self.kolMax == 0) {
                kolMax = 1
                zach(str: t23.text!)
                self.t23.attributedText = attributeString
            }
            if let text = t24.text { ocentp = Double(text)! * Double(10) } else { ocentp = 0 }
            if (ocentp == self.maxT && self.kolMax == 0) {
                kolMax = 1
                zach(str: t24.text!)
                self.t24.attributedText = attributeString
            }
            if let text = t25.text { ocentp = Double(text)! * Double(10) } else { ocentp = 0 }
            if (ocentp == self.maxT && self.kolMax == 0) {
                kolMax = 1
                zach(str: t25.text!)
                self.t25.attributedText = attributeString
            }
            if let text = t26.text { ocentp = Double(text)! * Double(10) } else { ocentp = 0 }
            if (ocentp == self.maxT && self.kolMax == 0) {
                kolMax = 1
                zach(str: t26.text!)
                self.t26.attributedText = attributeString
            }
            if let text = t27.text { ocentp = Double(text)! * Double(10) } else { ocentp = 0 }
            if (ocentp == self.maxT && self.kolMax == 0) {
                kolMax = 1
                zach(str: t27.text!)
                self.t27.attributedText = attributeString
            }
        }
    }
    
    func zachpmin() {
        kolMin = 0
        if (numPokaz == 1) {
            if let text = p11.text { ocentp = Double(text)! * Double(10) }
            if (ocentp == self.minP && self.kolMin == 0) {
                kolMin = 1
                zach(str: p11.text!)
                self.p11.attributedText = attributeString
            }
            if let text = p12.text { ocentp = Double(text)! * Double(10) }
            if (ocentp == self.minP && self.kolMin == 0) {
                kolMin = 1
                zach(str: p12.text!)
                self.p12.attributedText = attributeString
            }
            if let text = p13.text { ocentp = Double(text)! * Double(10) }
            if (ocentp == self.minP && self.kolMin == 0) {
                kolMin = 1
                zach(str: p13.text!)
                self.p13.attributedText = attributeString
            }
            if let text = p14.text { ocentp = Double(text)! * Double(10) }
            if (ocentp == self.minP && self.kolMin == 0) {
                kolMin = 1
                zach(str: p14.text!)
                self.p14.attributedText = attributeString
            }
            if let text = p15.text { ocentp = Double(text)! * Double(10) }
            if (ocentp == self.minP && self.kolMin == 0) {
                kolMin = 1
                zach(str: p15.text!)
                self.p15.attributedText = attributeString
            }
            if let text = p16.text { ocentp = Double(text)! * Double(10) }
            if (ocentp == self.minP && self.kolMin == 0) {
                kolMin = 1
                zach(str: p16.text!)
                self.p16.attributedText = attributeString
            }
            if let text = p17.text { ocentp = Double(text)! * Double(10) }
            if (ocentp == self.minP && self.kolMin == 0) {
                kolMin = 1
                zach(str: p17.text!)
                self.p17.attributedText = attributeString
            }
        }
        if(numPokaz == 2) {
            if let text = p21.text { ocentp = Double(text)! * Double(10) } else { ocentp = 0 }
            if (ocentp == self.minP && self.kolMin == 0) {
                kolMin = 1
                zach(str: p21.text!)
                self.p21.attributedText = attributeString
            }
            if let text = p22.text { ocentp = Double(text)! * Double(10) } else { ocentp = 0 }
            if (ocentp == self.minP && self.kolMin == 0) {
                kolMin = 1
                zach(str: p22.text!)
                self.p22.attributedText = attributeString
            }
            if let text = p23.text { ocentp = Double(text)! * Double(10) } else { ocentp = 0 }
            if (ocentp == self.minP && self.kolMin == 0) {
                kolMin = 1
                zach(str: p23.text!)
                self.p23.attributedText = attributeString
            }
            if let text = p24.text { ocentp = Double(text)! * Double(10) } else { ocentp = 0 }
            if (ocentp == self.minP && self.kolMin == 0) {
                kolMin = 1
                zach(str: p24.text!)
                self.p24.attributedText = attributeString
            }
            if let text = p25.text { ocentp = Double(text)! * Double(10) } else { ocentp = 0 }
            if (ocentp == self.minP && self.kolMin == 0) {
                kolMin = 1
                zach(str: p25.text!)
                self.p25.attributedText = attributeString
            }
            if let text = p26.text { ocentp = Double(text)! * Double(10) } else { ocentp = 0 }
            if (ocentp == self.minP && self.kolMin == 0) {
                kolMin = 1
                zach(str: p26.text!)
                self.p26.attributedText = attributeString
            }
            if let text = p27.text { ocentp = Double(text)! * Double(10) } else { ocentp = 0 }
            if (ocentp == self.minP && self.kolMin == 0) {
                kolMin = 1
                zach(str: p27.text!)
                self.p27.attributedText = attributeString
            }
        }
    }
    
    func zachpmax() {
        kolMax = 0
        if (numPokaz == 1) {
            if let text = p11.text { ocentp = Double(text)! * Double(10) }
            if (ocentp == self.maxP && self.kolMax == 0) {
                kolMax = 1
                zach(str: p11.text!)
                self.p11.attributedText = attributeString
            }
            if let text = p12.text { ocentp = Double(text)! * Double(10) }
            if (ocentp == self.maxP && self.kolMax == 0) {
                kolMin = 1
                zach(str: p12.text!)
                self.p12.attributedText = attributeString
            }
            if let text = p13.text { ocentp = Double(text)! * Double(10) }
            if (ocentp == self.maxP && self.kolMax == 0) {
                kolMax = 1
                zach(str: p13.text!)
                self.p13.attributedText = attributeString
            }
            if let text = p14.text { ocentp = Double(text)! * Double(10) }
            if (ocentp == self.maxP && self.kolMax == 0) {
                kolMax = 1
                zach(str: p14.text!)
                self.p14.attributedText = attributeString
            }
            if let text = p15.text { ocentp = Double(text)! * Double(10) }
            if (ocentp == self.maxP && self.kolMax == 0) {
                kolMax = 1
                zach(str: p15.text!)
                self.p15.attributedText = attributeString
            }
            if let text = p16.text { ocentp = Double(text)! * Double(10) }
            if (ocentp == self.maxP && self.kolMax == 0) {
                kolMax = 1
                zach(str: p16.text!)
                self.p16.attributedText = attributeString
            }
            if let text = p17.text { ocentp = Double(text)! * Double(10) }
            if (ocentp == self.maxP && self.kolMax == 0) {
                kolMax = 1
                zach(str: p17.text!)
                self.p17.attributedText = attributeString
            }
        }
        if (numPokaz == 2) {
        if let text = p21.text { ocentp = Double(text)! * Double(10) } else { ocentp = 0 }
            if (ocentp == self.maxP && self.kolMax == 0) {
                kolMax = 1
                zach(str: p21.text!)
                self.p21.attributedText = attributeString
            }
            if let text = p22.text { ocentp = Double(text)! * Double(10) } else { ocentp = 0 }
            if (ocentp == self.maxP && self.kolMax == 0) {
                kolMin = 1
                zach(str: p22.text!)
                self.p22.attributedText = attributeString
            }
            if let text = p23.text { ocentp = Double(text)! * Double(10) } else { ocentp = 0 }
            if (ocentp == self.maxP && self.kolMax == 0) {
                kolMax = 1
                zach(str: p23.text!)
                self.p23.attributedText = attributeString
            }
            if let text = p24.text { ocentp = Double(text)! * Double(10) } else { ocentp = 0 }
            if (ocentp == self.maxP && self.kolMax == 0) {
                kolMax = 1
                zach(str: p24.text!)
                self.p24.attributedText = attributeString
            }
            if let text = p25.text { ocentp = Double(text)! * Double(10) } else { ocentp = 0 }
            if (ocentp == self.maxP && self.kolMax == 0) {
                kolMax = 1
                zach(str: p25.text!)
                self.p25.attributedText = attributeString
            }
            if let text = p26.text { ocentp = Double(text)! * Double(10) } else { ocentp = 0 }
            if (ocentp == self.maxP && self.kolMax == 0) {
                kolMax = 1
                zach(str: p26.text!)
                self.p26.attributedText = attributeString
            }
            if let text = p27.text { ocentp = Double(text)! * Double(10) } else { ocentp = 0 }
            if (ocentp == self.maxP && self.kolMax == 0) {
                kolMax = 1
                zach(str: p27.text!)
                self.p27.attributedText = attributeString
            }
        }
    }
    
    func vistsav() {
        print("vistsav")
        self.URL_VIST = self.idhost! + self.URL_VIST2!
        let requestURL = NSURL(string: self.URL_VIST!)
        let request = NSMutableURLRequest(url: requestURL! as URL)
        request.httpMethod = "POST"
        let postParameters = "idvistuplenie=" + String (self.idvistuplenie)
        
        request.httpBody = postParameters.data(using: String.Encoding.utf8)
        let task = URLSession.shared.dataTask(with: request as URLRequest){
            data, response, error in
            do {
                if (error != nil) {
                    DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1), execute: {
                        self.vistsav()
                    })
                } else {
                    self.vistup = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.allowFragments) as? NSDictionary
                    self.successStr = self.vistup["success"] as? Int
                    if (self.successStr == 0) {
                        DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1), execute: {
                            self.vistsav()
                        })
                    } else {
                        self.idvis()
                    }
                }
            } catch {
                print("error = ", error)
               // self.idvis()
            }
        }
        
        task.resume()
    }
    
    func idvis() {
        print("TUT")
        self.URL_VISTUP = self.idhost! + self.URL_VISTUPLENIE2
        let requestURL = NSURL(string: self.URL_VISTUP!)
        //creating NSMutableURLRequest
        let request = NSMutableURLRequest(url: requestURL! as URL)
        self.vistuplenie = NSDictionary()
        //creating a task to send the post request
        let task = URLSession.shared.dataTask(with: request as URLRequest){
            data, response, error in
            //parsing the response
            do {
                //converting resonse to NSDictionary
                if (error != nil) {
                    DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1), execute: {
                        self.idvis()
                    })
                } else {
                    self.vistuplenie =  try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.allowFragments) as? NSDictionary
                    self.idvistup = self.vistuplenie["success"] as? String
                    
                    if (self.idvistup != "") {
                        self.success = Int (self.idvistup)
                        self.idphumse = self.vistuplenie["idphumse"] as? String
                        switch Int(self.idphumse)! {
                        case 0-15:
                            self.sys = 1
                            break
                        case 16:
                            self.sys = 2
                            break
                        case 17, 18:
                            self.sys = 10
                            self.freestyleViv = 2
                            break
                        case 19:
                            self.sys = 10
                            self.freestyleViv = 1
                            break
                        case 20:
                            self.sys = 10
                            self.freestyleViv = 3
                            break
                        default:
                            break
                        }
//                        if (Int(self.idphumse)! > 15) {
//                            if (Int(self.idphumse)! > 16) {
//                                self.sys = 10
//                            } else {
//                                self.sys=2
//                            }
//                        }
                    } else {
                        self.success = 0
                    }
                }
            } catch {
                print(error)
            }
            
            if (self.success != 0) {
                DispatchQueue.main.async {
                    switch (self.sys) {
                    case 1:
                        if (self.vistocen == 2) {
                            let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
                            let nextViewController = storyBoard.instantiateViewController(withIdentifier: "ocenLineView") as! OcenController
                            nextViewController.idhost = self.idhost
                            nextViewController.sudNum = self.sudNum
                            nextViewController.kolSud = self.kolSud
                            nextViewController.vistocen = self.vistocen
                            nextViewController.sys = self.sys
                            self.navigationController?.pushViewController(nextViewController, animated: true)
                        } else {
                            let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
                            let viewController = storyBoard.instantiateViewController(withIdentifier: "ocenButtonView") as! ButtonOcenController
                            viewController.idhost = self.idhost
                            viewController.sudNum = self.sudNum
                            viewController.kolSud = self.kolSud
                            viewController.sys = self.sys
                            viewController.vistocen = self.vistocen
                            self.navigationController?.pushViewController(viewController, animated: true)
                        }
                        break
                    case 2:
                        let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
                        let viewController = storyBoard.instantiateViewController(withIdentifier: "ocenFreestyleView") as! FreestyleController
                        viewController.idhost = self.idhost
                        viewController.sudNum = self.sudNum
                        viewController.kolSud = self.kolSud
                        viewController.sys = self.sys
                        self.navigationController?.pushViewController(viewController, animated: true)
                        
                    case 10:
                        let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
                        let viewController = storyBoard.instantiateViewController(withIdentifier: "ocenHapkiFreestyleView") as! HapkiFreestyleController
                        viewController.freestyleViv = self.freestyleViv
                        viewController.idhost = self.idhost
                        viewController.sudNum = self.sudNum
                        viewController.kolSud = self.kolSud
                        viewController.sys = self.sys
                        self.navigationController?.pushViewController(viewController, animated: true)
                        
                    default: break
                        
                    }

                }
                
            } else {
                DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1), execute: {
                    self.idvis()
                })
            }
            
        }
        
        task.resume()
    }
    
    func labelOcen() {
        
        switch kolSud {
        case 3:
            t23.isHidden = false
            t24.isHidden = false
            t25.isHidden = false
            
            p23.isHidden = false
            p24.isHidden = false
            p25.isHidden = false
            
        case 5:
            t22.isHidden = false
            t23.isHidden = false
            t24.isHidden = false
            t25.isHidden = false
            t26.isHidden = false
            
            p22.isHidden = false
            p23.isHidden = false
            p24.isHidden = false
            p25.isHidden = false
            p26.isHidden = false
            
        case 7:
            t21.isHidden = false
            t22.isHidden = false
            t23.isHidden = false
            t24.isHidden = false
            t25.isHidden = false
            t26.isHidden = false
            t27.isHidden = false
            
            p21.isHidden = false
            p22.isHidden = false
            p23.isHidden = false
            p24.isHidden = false
            p25.isHidden = false
            p26.isHidden = false
            p27.isHidden = false
        default:
            break
        }
        
    }
    
    func ocenki2() {
        self.zap1 = 0
        self.zap2 = 0
        self.zap3 = 0
        self.zap4 = 0
        self.zap5 = 0
        self.zap6 = 0
        self.zap7 = 0
        self.sumT = []
        self.sumP = []
        self.URL_OCENKI = self.idhost! + self.URL_OCENKI2!
        let requestURL = NSURL(string: self.URL_OCENKI!)
        let request = NSMutableURLRequest(url: requestURL! as URL)
        request.httpMethod = "POST"
        self.postParameters = "vistuplenie_idvistuplenie=" + String (self.idvistuplenie)
        request.httpBody = postParameters.data(using: String.Encoding.utf8)
        self.myJSON2 = NSDictionary()
        
        //creating a task to send the post request
        let task = URLSession.shared.dataTask(with: request as URLRequest) {
            data, response, error in
            do {
                if (error != nil) {
                    DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1), execute: {
                        self.ocenki2()
                    })
                } else {
                    self.myJSON2 =  try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.allowFragments) as? NSDictionary
                    self.success = (self.myJSON2["success"]) as? Int
                    if (self.success > 0) {
                        self.row = (self.myJSON2["row"]) as? Int
                        self.ocenki_arr2 = (self.myJSON2["ocenki"]) as? NSArray
                        DispatchQueue.main.async {
                        switch (self.kolSud) {
                            case 3:
                            for i in self.ocenki_arr2 {
                                self.tp = i as? NSDictionary
                                switch (self.tp["ocenki_nomsud"] as? String) {
                                case "1":
                                    if (self.zap1 == 0) {
                                        self.zap1 = 1
                                        self.ocen()
                                        self.t23.text = String (self.ot/Double(10))
                                        self.p23.text = String (self.op/Double(10))
                                    }
                                    break
                                case "2":
                                    if (self.zap2 == 0) {
                                        self.zap2 = 1
                                        self.ocen()
                                        self.t24.text = String (self.ot/Double(10))
                                        self.p24.text = String (self.op/Double(10))
                                    }
                                    break
                                case "3":
                                    if (self.zap3 == 0) {
                                        self.zap3 = 1
                                        self.ocen()
                                        self.t25.text = String (self.ot/Double(10))
                                        self.p25.text = String (self.op/Double(10))
                                    }
                                    break
                                default:
                                    break
                                }
                            }
                            break
                            
                            case 5:
                            for i in self.ocenki_arr2 {
                                self.tp = i as? NSDictionary
                                switch (self.tp["ocenki_nomsud"] as? String) {
                                case "1":
                                    if (self.zap1 == 0) {
                                        self.zap1 = 1
                                        self.ocen()
                                        self.t22.text = String (self.ot/Double(10))
                                        self.p22.text = String (self.op/Double(10))
                                    }
                                    break
                                case "2":
                                    if (self.zap2 == 0) {
                                        self.zap2 = 1
                                        self.ocen()
                                        self.t23.text = String (self.ot/Double(10))
                                        self.p23.text = String (self.op/Double(10))
                                    }
                                    break
                                case "3":
                                    if (self.zap3 == 0) {
                                        self.zap3 = 1
                                        self.ocen()
                                        self.t24.text = String (self.ot/Double(10))
                                        self.p24.text = String (self.op/Double(10))
                                    }
                                    break
                                case "4":
                                    if (self.zap4 == 0) {
                                        self.zap4 = 1
                                        self.ocen()
                                        self.t25.text = String (self.ot/Double(10))
                                        self.p25.text = String (self.op/Double(10))
                                    }
                                    break
                                case "5":
                                    if (self.zap5 == 0) {
                                        self.zap5 = 1
                                        self.ocen()
                                        self.t26.text = String (self.ot/Double(10))
                                        self.p26.text = String (self.op/Double(10))
                                    }
                                    break
                                default:
                                    break
                                }
                            }
                            
                            case 7:
                                for i in self.ocenki_arr2 {
                                    self.tp = i as? NSDictionary
                                    switch (self.tp["ocenki_nomsud"] as? String) {
                                    case "1":
                                        if (self.zap1 == 0) {
                                            self.zap1 = 1
                                            self.ocen()
                                            self.t21.text = String (self.ot/Double(10))
                                            self.p21.text = String (self.op/Double(10))
                                        }
                                        break
                                    case "2":
                                        if (self.zap2 == 0) {
                                            self.zap2 = 1
                                            self.ocen()
                                            self.t22.text = String (self.ot/Double(10))
                                            self.p22.text = String (self.op/Double(10))
                                        }
                                        break
                                    case "3":
                                        if (self.zap3 == 0) {
                                            self.zap3 = 1
                                            self.ocen()
                                            self.t23.text = String (self.ot/Double(10))
                                            self.p23.text = String (self.op/Double(10))
                                        }
                                        break
                                    case "4":
                                        if (self.zap4 == 0) {
                                            self.zap4 = 1
                                            self.ocen()
                                            self.t24.text = String (self.ot/Double(10))
                                            self.p24.text = String (self.op/Double(10))
                                        }
                                        break
                                    case "5":
                                        if (self.zap5 == 0) {
                                            self.zap5 = 1
                                            self.ocen()
                                            self.t25.text = String (self.ot/Double(10))
                                            self.p25.text = String (self.op/Double(10))
                                        }
                                        break
                                    case "6":
                                        if (self.zap6 == 0) {
                                            self.zap6 = 1
                                            self.ocen()
                                            self.t26.text = String (self.ot/Double(10))
                                            self.p26.text = String (self.op/Double(10))
                                        }
                                        break
                                    case "7":
                                        if (self.zap7 == 0) {
                                            self.zap7 = 1
                                            self.ocen()
                                            self.t27.text = String (self.ot/Double(10))
                                            self.p27.text = String (self.op/Double(10))
                                        }
                                        break
                                    default:
                                        break
                                    }
                                }
                                
                            break
                            
                        default:
                            
                            for i in self.ocenki_arr2 {
                                self.tp = i as? NSDictionary
                                switch (self.tp["ocenki_nomsud"] as? String) {
                                case "1":
                                    self.ocen()
                                    self.t24.text = String (self.ot/Double(10))
                                    self.p24.text = String (self.op/Double(10))
                                    break
                                default: break
                                }
                            }
                            break
                            }
                        }
                    }
                }
                    if (self.kolSud == self.row) {
                         DispatchQueue.main.async {self.tot1.isHidden = false; self.tot2.isHidden = false; self.total.isHidden = false; self.numPokaz = 2; self.summa()}
                    } else {
                        DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1), execute: {
                            self.ocenki2()
                        })
                    }
                

                } catch {
                    print(error)
                }

        }
        task.resume()
    }
}
